package com.bangcommunity.bbframe.sdm.aop.handler;

import com.bangcommunity.bbframe.common.utils.lang.AnnotationUtils;
import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import com.bangcommunity.bbframe.sdm.aop.RecordLog;
import com.bangcommunity.bbframe.sdm.aop.SkipLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;

import java.lang.reflect.Method;

/**
 * Created by tanghc on 17/1/18.
 */
public class AopLogPreHandler extends PreHandler {
    private String keyword;
    private Logger logger;

    public AopLogPreHandler(String keyword, Logger logger) {
        this.keyword = keyword;
        this.logger = logger;
    }

    @Override
    protected boolean doPreHandler(ProceedingJoinPoint pjp, Object[] args, Method method, String className) {
        SkipLog skipLogAnnotation = AnnotationUtils.findAnnotation(method, SkipLog.class);
        if (null != skipLogAnnotation) {
            return true;
        } else {
            skipLogAnnotation = AnnotationUtils.findAnnotation(pjp.getTarget().getClass(), SkipLog.class);
            if (null != skipLogAnnotation) {
                return true;
            }
        }
        RecordLog recordLogAnnotation = AnnotationUtils.findAnnotation(method, RecordLog.class);
        if (null == recordLogAnnotation) {
            recordLogAnnotation = AnnotationUtils.findAnnotation(pjp.getTarget().getClass(), RecordLog.class);
            if (null == recordLogAnnotation) {
                return true;
            }
        }
        String argStr = StringUtils.join(args, "||");
        logger.info("begin {} {} {}: param={}", new Object[] { keyword, className, method.getName(), argStr });
        return true;
    }
}
